/*
 * Copyright (C) 2022 xuexiangjys(xuexiangjys@163.com)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *       http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 */

package com.arthas.psimobile.fragment.report;

import android.app.Activity;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.arthas.psimobile.fragment.protocol.PrivacyProtocolFragment;
import com.arthas.psimobile.fragment.protocol.ServiceProtocolFragment;
import com.arthas.psimobile.utils.Utils;
import com.arthas.psimobile.utils.XToastUtils;
import com.luck.picture.lib.PictureSelector;
import com.luck.picture.lib.config.PictureConfig;
import com.xuexiang.psimobile.R;
import com.arthas.psimobile.core.BaseFragment;
import com.luck.picture.lib.entity.LocalMedia;
import com.xuexiang.psimobile.databinding.FragmentApplyReportBinding;
import com.xuexiang.xpage.annotation.Page;
import com.xuexiang.xpage.enums.CoreAnim;
import com.xuexiang.xui.widget.actionbar.TitleBar;

import java.util.ArrayList;
import java.util.List;

/**
 * 申请报告
 *
 * @author Arthas
 * @since 2022/4/3 18:50
 */
@Page(anim = CoreAnim.none, name = "申请报告")
public class ApplyReportFragment extends BaseFragment<FragmentApplyReportBinding> implements View.OnClickListener, ImageSelectGridAdapter.OnAddPicClickListener {

    RecyclerView posiRecyclerView;
    RecyclerView negaRecyclerView;

    private ImageSelectGridAdapter mPosiAdapter;
    private ImageSelectGridAdapter mNegaAdapter;

    private List<LocalMedia> mPosiList = new ArrayList<>();
    private List<LocalMedia> mNegaList = new ArrayList<>();

    @Override
    protected TitleBar initTitle() {
        return null;
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();

        if (id == R.id.id_card_posi_upload) {
            Utils.getPictureSelector(this)
                    .selectionMedia(mPosiList)
                    .forResult(27290);
        } else if (id == R.id.id_card_nega_upload) {
            Utils.getPictureSelector(this)
                    .selectionMedia(mNegaList)
                    .forResult(27291);
        } else if (id == R.id.tv_privacy_protocol) {
            openNewPage(PrivacyProtocolFragment.class);
        } else if (id == R.id.tv_disclaimer) {
            openNewPage(ServiceProtocolFragment.class);
        } else if (id == R.id.btn_apply_report) {
            openNewPage(ReportViewFragment.class);
        }
    }

    @NonNull
    @Override
    protected FragmentApplyReportBinding viewBindingInflate(LayoutInflater inflater, ViewGroup container) {
        return FragmentApplyReportBinding.inflate(inflater, container, false);
    }

    @Override
    protected void initViews() {
        // 身份证国徽面
        GridLayoutManager posiManager = new GridLayoutManager(getActivity(), 4, RecyclerView.VERTICAL, false);
        posiRecyclerView = findViewById(R.id.id_card_posi);
        posiRecyclerView.setLayoutManager(posiManager);
        posiRecyclerView.setAdapter(mPosiAdapter = new ImageSelectGridAdapter(getActivity(), this));
        mPosiAdapter.setSelectList(mPosiList);
        mPosiAdapter.setSelectMax(1);
        mPosiAdapter.setOnItemClickListener((position, v) -> PictureSelector.create(ApplyReportFragment.this).themeStyle(R.style.XUIPictureStyle).openExternalPreview(position, mPosiList));

        // 身份证人像面
        GridLayoutManager negaManager = new GridLayoutManager(getActivity(), 4, RecyclerView.VERTICAL, false);
        negaRecyclerView = findViewById(R.id.id_card_nega);
        negaRecyclerView.setLayoutManager(negaManager);
        negaRecyclerView.setAdapter(mNegaAdapter = new ImageSelectGridAdapter(getActivity(), this));
        mNegaAdapter.setSelectList(mNegaList);
        mNegaAdapter.setSelectMax(1);
        mNegaAdapter.setOnItemClickListener((position, v) -> PictureSelector.create(ApplyReportFragment.this).themeStyle(R.style.XUIPictureStyle).openExternalPreview(position, mNegaList));
    }



    @Override
    public void onAddPicClick() {

    }

    @Override
    protected void initListeners() {
        binding.idCardNegaUpload.setOnClickListener(this);
        binding.idCardPosiUpload.setOnClickListener(this);
        binding.btnApplyReport.setOnClickListener(this);
        binding.tvPrivacyProtocol.setOnClickListener(this);
        binding.tvDisclaimer.setOnClickListener(this);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            switch (requestCode) {
                case 27290:
                    mPosiList = PictureSelector.obtainMultipleResult(data);
                    mPosiAdapter.setSelectList(mPosiList);
                    mPosiAdapter.notifyDataSetChanged();
                    break;
                case 27291:
                    mNegaList = PictureSelector.obtainMultipleResult(data);
                    mNegaAdapter.setSelectList(mNegaList);
                    mNegaAdapter.notifyDataSetChanged();
                default:
                    break;
            }
        }
    }
}
